# frozen_string_literal: true

RSpec.shared_examples 'tracks namespace creation experiments' do
  context 'with experiments' do
    context 'with experiment premium_trial_positioning', :experiment do
      before do
        stub_experiments(premium_trial_positioning: :control)
        allow_next_instance_of(::Projects::CreateService) do |service|
          allow(service).to receive(:after_create_actions)
        end
      end

      it 'uses onboarding user status to determine experiment exclusion' do
        onboarding_user_status = instance_double(::Onboarding::UserStatus)
        allow(::Onboarding::UserStatus).to receive(:new).with(user).and_return(onboarding_user_status)
        allow(onboarding_user_status).to receive_messages(
          exclude_from_first_orders_experiments?: true,
          apply_trial?: false
        )

        expect_any_instance_of(PremiumTrialPositioningExperiment) do |instance|
          expect(instance).to receive(:exclude!)
          expect(instance).to receive(:track).with(:assignment, namespace: an_instance_of(Group))
        end

        expect(execute).to be_success
      end
    end

    context 'with experiment lightweight_trial_registration_redesign' do
      let(:lightweight_experiment) { instance_double(ApplicationExperiment) }
      let(:premium_positioning_experiment) { instance_double(ApplicationExperiment) }
      let(:premium_message_experiment) { instance_double(ApplicationExperiment) }
      let(:trial_registration_hierarchy_education_experiment) { instance_double(ApplicationExperiment) }

      it 'tracks experiment assignment' do
        allow_next_instance_of(::Projects::CreateService) do |service|
          allow(service).to receive(:after_create_actions)
        end

        allow(premium_positioning_experiment).to receive(:exclude!)
        allow(premium_message_experiment).to receive(:exclude!)
        allow(trial_registration_hierarchy_education_experiment).to receive(:exclude!)

        expect_next_instance_of(described_class) do |service|
          expect(service).to receive(:experiment).with(:lightweight_trial_registration_redesign,
            actor: user).and_return(lightweight_experiment)
          expect(service).to receive(:experiment).with(:premium_trial_positioning,
            actor: user).and_yield(premium_positioning_experiment)
          expect(service).to receive(:experiment).with(:premium_message_during_trial,
            namespace: an_instance_of(Group)).and_yield(premium_message_experiment)
          expect(service).to receive(:experiment).with(:trial_registration_hierarchy_education,
            actor: user).and_yield(trial_registration_hierarchy_education_experiment)
        end

        expect(lightweight_experiment).to receive(:track).with(:assignment, namespace: an_instance_of(Group))
        expect(premium_positioning_experiment).to receive(:track).with(:assignment, namespace: an_instance_of(Group))
        expect(trial_registration_hierarchy_education_experiment).to receive(:track).with(:assignment,
          namespace: an_instance_of(Group))

        expect(execute).to be_success
      end
    end
  end
end
